#include <stdio.h>
#include <stdlib.h>

#include <string.h>

#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>
#define SEM_SET_PATHNAME "."
#define SEM_PRO_ID 103


union semun_ex {
    int val;
};

int main(int argc, char const *argv[])
{

    union semun_ex se;
    key_t key = ftok(SEM_SET_PATHNAME, SEM_PRO_ID);
    if (key == -1) {
        perror("ftok");
        exit(EXIT_FAILURE);
    }

    //创建信号量集合
    int semId = semget(key, 1, IPC_CREAT | 0644);
    if (semId == -1) {
        perror("semget error");
        exit(EXIT_FAILURE);
    }
    se.val = 1;
    // 初始化信号量值 , 具体值为1
    int ret = semctl(semId, 0, SETVAL, se); // 信号量编号 设置信号量值
    if (ret == -1 ) {
        perror("semctl error");
        exit(EXIT_FAILURE);
    }
    return 0;
}
